package com.jiecao.news.jiecaonews.adapters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiecao.news.jiecaonews.R;
import com.jiecao.news.jiecaonews.pojo.NewsListItem;
import com.jiecao.news.jiecaonews.util.cache.ImageLoader;


public class NewsAdapter extends BaseAdapter {
	private static final String TAG = "NewsAdapter";
	LayoutInflater mInflater;
	private ArrayList<NewsListItem> mItems;// 包含从服务端或者缓存端取到的信息
	private ImageLoader mImageLoader;// 数据加载

	public NewsAdapter(LayoutInflater Inflater,
			ArrayList<NewsListItem> arrayList, ImageLoader imageLoader) {
		mInflater = Inflater;
		mItems = arrayList;
		mImageLoader = imageLoader;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NewsListItem item = mItems.get(position); // ListView的Item
		convertView = makeNewsView(item);
		return convertView;
	}


	private View makeNewsView(NewsListItem item) {
		View convertView;
		NoticeNewsViewHolder holder;

		convertView = mInflater.inflate(R.layout.news_item, null);
		holder = new NoticeNewsViewHolder();
		holder.listRelativeLayout = (RelativeLayout) convertView
				.findViewById(R.id.news_list_layout);
		holder.commentNum = (TextView) convertView
				.findViewById(R.id.news_comment_num_view);
		holder.date = (TextView) convertView
				.findViewById(R.id.news_date_view);
		holder.listImg = (ImageView) convertView
				.findViewById(R.id.news_list_img);
		holder.title = (TextView) convertView
				.findViewById(R.id.news_title_view);
		holder.des=(TextView)convertView.findViewById(R.id.news_des_view);
		convertView.setTag(holder);

		holder.commentNum
				.setText(""
						+ (item.getShareNum() == null ? 0 : item
								.getShareNum()));
		System.out.println("num"+item.getShareNum());
		holder.date.setText((String) (item.getPublishTime() == null ? ""
				: dealWithPublishDate(item.getPublishTime())));
		holder.title.setText((String) (item.getTitle() == null ? "" : item
				.getTitle()));
		holder.listImg.setImageResource(R.drawable.news_list_default); // 设置默认图片
		mImageLoader.addTask((String) item.getIcon(), holder.listImg); // 添加任务
		holder.des.setText((String)(item.getIntro()==null?"":item.getIntro()));
		return convertView;
	}



	/**
	 * nitice_news 的一个条目所包含的东西
	 * 
	 * @author lidongwei
	 * 
	 */
	class NoticeNewsViewHolder {
		RelativeLayout listRelativeLayout;
		ImageView listImg;
		TextView title;
		TextView des;
		TextView date;
		TextView commentNum;
	}

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mItems.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private String dealWithPublishDate(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = sdf.parse(dateStr);
			Date now = new Date();
			// 是当天
			if (date.getDay() == now.getDay()
					&& date.getMonth() == now.getMonth()
					&& date.getYear() == now.getYear()) {
				return dateStr.split(" ")[1] + " ";
			} else {
				return dateStr.split(" ")[0] + " ";
			}

		} catch (ParseException e) {

			e.printStackTrace();
			return "";
		}
	}

}
